package com.wang.wx.utils;


import com.wang.wx.constants.BatchConstants;
import com.wang.wx.constants.HeadConstants;
import com.wang.wx.constants.IndexConstants;
import com.wang.wx.entity.LyUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.*;
import java.util.Iterator;


@Slf4j
public class CsvUtils {


    /**
     * 解析csv文件并转成bean（方法二）
     *
     * @param filePath csv文件
     * @return 数组
     */
    public static void parsingCsvDataMethod(String filePath) {
//        List<LyUser> lyUserList = new ArrayList<>(20000);

        try (Reader reader = new FileReader(filePath);
             CSVParser csvParser = CSVFormat.DEFAULT
                     .withHeader(
                             HeadConstants.CSV_HEAD
                     )
//                     .withFirstRecordAsHeader()
                     .withIgnoreHeaderCase()
                     .withTrim()
                     .parse(reader)) {
            Iterator<CSVRecord> iterator = csvParser.iterator();
            if (iterator.hasNext()){
                iterator.next();    //跳过第一行
            }
//            int i = 0;
            //iterator.hasNext()有可能UncheckedIOException
            while (true) {
                try {
                    boolean hasNext = iterator.hasNext();
                    if (!hasNext) {
                        break;
                    }
                } catch (UncheckedIOException e) {
                    continue;
                }
                
//                if (i > 333) {
//                    System.out.println(i);
//                }
//                i++;
                //赋值代码
                setValue(iterator);
            }
            
        } catch (IOException e) {
            log.error("解析csv文件失败",e);
            throw new RuntimeException("解析csv文件失败");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private static void setValue(Iterator<CSVRecord> iterator) throws InterruptedException {
        CSVRecord csvRecord = iterator.next();
        if (csvRecord.size() < 62) {
            return;
        }
        //如果领英用户名是空的话，直接跳过
        if (csvRecord.get(IndexConstants.LyIndexConstants.LINKEDIN_USER_NAME).isEmpty()) {
            return;
        }
        
        LyUser lyUser = new LyUser();
        lyUser.setFullName(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.FULL_NAME)));
        lyUser.setIndustry(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.INDUSTRY)));
        lyUser.setJobTitle(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.JOB_TITLE)));
        lyUser.setSubRole(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.SUB_ROLE)));
        lyUser.setIndustry2(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.INDUSTRY2)));
        lyUser.setEmails(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.EMAILS)));
        lyUser.setMobile(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.MOBILE)));
        lyUser.setPhoneNumbers(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.PHONE_NUMBERS).replace("+","")));
        lyUser.setCompanyName(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_NAME)));
        lyUser.setCompanyIndustry(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_INDUSTRY)));
        lyUser.setCompanyWebsite(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_WEBSITE)));
        lyUser.setCompanySize(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_SIZE)));
        if (csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_FOUNDED) != null && csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_FOUNDED).matches("\\b(19[0-9]{2}|20[0-9]{2})\\b\n")) {
            lyUser.setCompanyFounded(Integer.valueOf(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_FOUNDED)));
        }
        lyUser.setLocation(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.LOCATION)));
        lyUser.setLocality(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.LOCALITY)));
        lyUser.setMetro(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.METRO)));
        lyUser.setRegion(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.REGION)));
        lyUser.setSkills(checkIsEmpty(csvRecord.get(IndexConstants.LyIndexConstants.SKILLS)));
        lyUser.setFirstName(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.FIRST_NAME)));
        lyUser.setMiddleInitial(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.MIDDLE_INITIAL)));
        lyUser.setMiddleName(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.MIDDLE_NAME)));
        lyUser.setLastName(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.LAST_NAME)));
        if (csvRecord.get(IndexConstants.LyIndexConstants.BIRTH_YEAR) != null && csvRecord.get(IndexConstants.LyIndexConstants.BIRTH_YEAR).matches("\\b(19[0-9]{2}|20[0-9]{2})\\b\n")) {
            lyUser.setBirthYear(Integer.valueOf(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.BIRTH_YEAR))));
        }
        lyUser.setBirthDate(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.BIRTH_DATE)));
        lyUser.setGender(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.GENDER)));
        lyUser.setLinkedinUrl(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.LINKEDIN_URL)));
        lyUser.setLinkedinUserName(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.LINKEDIN_USER_NAME)));
        lyUser.setFacebookUrl(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.FACEBOOK_URL)));
        lyUser.setFacebookUserName(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.FACEBOOK_USER_NAME)));
        lyUser.setTwitterUrl(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.TWITTER_URL)));
        lyUser.setTwitterUserName(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.TWITTER_USER_NAME)));
        lyUser.setGithubUrl(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.GITHUB_URL)));
        lyUser.setGithubUserName(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.GITHUB_USER_NAME)));
        lyUser.setCompanyLinkedinUrl(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_LINKEDIN_URL)));
        lyUser.setCompanyFacebookUrl(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_FACEBOOK_URL)));
        lyUser.setCompanyTwitterUrl(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_TWITTER_URL)));
        lyUser.setCompanyLocationName(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_LOCATION_NAME)));
        lyUser.setCompanyLocationLocality(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_LOCATION_LOCALITY)));
        lyUser.setCompanyLocationMetro(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_LOCATION_METRO)));
        lyUser.setCompanyLocationRegion(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_LOCATION_REGION)));
        lyUser.setCompanyLocationGeo(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_LOCATION_GEO)));
        lyUser.setCompanyLocationStreetAddress(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_LOCATION_STREET_ADDRESS)));
        lyUser.setCompanyLocationAddressLine2(checkIsEmpty(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_LOCATION_ADDRESS_LINE2)));
        lyUser.setCompanyLocationPostalCode(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_LOCATION_POSTAL_CODE)));
        lyUser.setCompanyLocationCountry(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_LOCATION_COUNTRY)));
        lyUser.setCompanyLocationContinent(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COMPANY_LOCATION_CONTINENT)));
        lyUser.setLastUpdated(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.LAST_UPDATED)));
        lyUser.setStartDate(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.START_DATE)));
        lyUser.setJobSummary(checkIsEmpty(csvRecord.get(IndexConstants.LyIndexConstants.JOB_SUMMARY)));
        lyUser.setLocationCountry(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.LOCATION_COUNTRY)));
        lyUser.setLocationContinent(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.LOCATION_CONTINENT)));
        lyUser.setStreetAddress(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.STREET_ADDRESS)));
        lyUser.setAddressLine2(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.ADDRESS_LINE2)));
        lyUser.setPostalCode(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.POSTAL_CODE)));
        lyUser.setLocationGeo(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.LOCATION_GEO)));
        lyUser.setLinkedinConnections(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.LINKEDIN_CONNECTIONS)));
        lyUser.setInferredSalary(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.INFERRED_SALARY)));
        lyUser.setYearsExperience(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.YEARS_EXPERIENCE)));
        lyUser.setSummary(checkIsEmpty(csvRecord.get(IndexConstants.LyIndexConstants.SUMMARY)));
        lyUser.setCountries(checkIsEmptyAndLength(csvRecord.get(IndexConstants.LyIndexConstants.COUNTRIES)));
        lyUser.setInterests(checkIsEmpty(csvRecord.get(IndexConstants.LyIndexConstants.INTERESTS)));
//        System.out.println(lyUser);
        BatchConstants.LY_QUEUE.put(lyUser);
    }
    
    public static String checkIsEmptyAndLength(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        if (str.length() > 255) {
            return str.substring(0, 255);
        }
        return str;
    }

    public static String checkIsEmpty(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        return str;
    }


    public static void main(String[] args) throws FileNotFoundException {
        parsingCsvDataMethod("D:\\Downloads\\Alabama.csv");
//        lyUserList.forEach(System.out::println);
//        
    }
}
